<template>
  <el-card shadow="hover" class="mt-10">
    <el-button
      type="primary"
      :icon="Plus"
      :disabled="!categoryStore.category3Id"
      @click="showAddSpu"
    >
      添加SPU
    </el-button>
    <el-table :data="spuList" border class="mt-10">
      <el-table-column label="序号" type="index" align="center" width="80" />
      <el-table-column label="SPU名称" prop="spuName" />
      <el-table-column label="SPU描述" prop="description" />
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button
            type="primary"
            :icon="Plus"
            size="small"
            title="添加SKU"
            @click="showAddSku(row)"
          />
          <el-button
            type="warning"
            :icon="Edit"
            size="small"
            title="修改SPU"
            @click="showUpdateSpu(row)"
          />
          <el-button
            type="info"
            :icon="InfoFilled"
            size="small"
            title="查看SKU列表"
          />
          <el-button
            type="danger"
            :icon="Delete"
            size="small"
            title="删除SPU"
            @click="deleteSpu(row)"
          />
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="mt-20"
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :total="total"
      :page-sizes="[3, 6, 9, 12]"
      layout="prev, pager, next, jumper, ->, sizes, total"
      @current-change="getSpuList"
      @size-change="getSpuList"
    />
  </el-card>
</template>

<script lang="ts">
export default {
  name: "SpuList",
};
</script>

<script lang="ts" setup>
import { Delete, InfoFilled, Plus, Edit } from "@element-plus/icons-vue";
import { ref, watch } from "vue";
import { getSpuListApi, deleteSpuApi } from "@/api/product/spu";
import { useCategoryStore } from "@/stores/category";
import { useSpuStore } from "@/stores/spu";
import type { SpuList, AllSpuItem } from "@/api/product/model/spuModel";
import { ElMessage } from "element-plus";

const currentPage = ref(1);
const pageSize = ref(5);
const total = ref(0);
const spuList = ref<SpuList>([]);

const categoryStore = useCategoryStore();
const getSpuList = async () => {
  const res = await getSpuListApi({
    page: currentPage.value,
    limit: pageSize.value,
    category3Id: categoryStore.category3Id as number,
  });
  spuList.value = res.records;
  total.value = res.total;
};

watch(
  () => categoryStore.category3Id,
  (category3Id) => {
    if (!category3Id) {
      spuList.value = [];
      currentPage.value = 1;
      pageSize.value = 3;
      total.value = 0;
      return;
    }
    getSpuList();
  },
  { immediate: true }
);

const emit = defineEmits(["setIsShow"]);

const spuStore = useSpuStore();
const showAddSpu = () => {
  // 添加要重置内容
  spuStore.$reset();
  emit("setIsShow", 2);
};

const showUpdateSpu = (row: AllSpuItem) => {
  spuStore.spuItem = row;
  emit("setIsShow", 2);
};

const showAddSku = (row: AllSpuItem) => {
  spuStore.spuItem = row;
  emit("setIsShow", 3);
};

const deleteSpu = async (row: AllSpuItem) => {
  await deleteSpuApi(row.id as number);
  ElMessage.success("删除成功");
  getSpuList();
};
</script>

<style scoped></style>
